/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy-layui
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy-layui
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.sys.modular.sysExpertReservation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import vip.xiaonuo.core.context.login.LoginContextHolder;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.login.SysLoginUser;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.sys.modular.WebSocketController;
import vip.xiaonuo.sys.modular.notice.enums.SysNoticeUserStatusEnum;
import vip.xiaonuo.sys.modular.sysExpertReservation.entity.ExpertReservationlog;
import vip.xiaonuo.sys.modular.sysExpertReservation.entity.SysExpertReservation;
import vip.xiaonuo.sys.modular.sysExpertReservation.enums.SysExpertReservationExceptionEnum;
import vip.xiaonuo.sys.modular.sysExpertReservation.mapper.ExpertReservationlogMapper;
import vip.xiaonuo.sys.modular.sysExpertReservation.mapper.SysExpertReservationMapper;
import vip.xiaonuo.sys.modular.sysExpertReservation.param.SysExpertReservationParam;
import vip.xiaonuo.sys.modular.sysExpertReservation.service.ExpertReservationlogService;
import vip.xiaonuo.sys.modular.sysExpertReservation.service.SysExpertReservationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

/**
 * 专家预约表service接口实现类
 *
 * @author zhang
 * @date 2024-06-18 11:36:11
 */
@Service
public class SysExpertReservationServiceImpl extends ServiceImpl<SysExpertReservationMapper, SysExpertReservation> implements SysExpertReservationService {

    @Resource
    private SysExpertReservationMapper sysExpertReservationMapper;
    @Resource
    private ExpertReservationlogMapper expertReservationlogMapper;
    @Resource
    private ExpertReservationlogService expertReservationlogService;
    @Override
    public PageResult<SysExpertReservation> page(SysExpertReservationParam sysExpertReservationParam) {
        QueryWrapper<SysExpertReservation> queryWrapper = new QueryWrapper<>();
        Page<SysExpertReservation> pageRecode = new Page<>();
        if (ObjectUtil.isNotNull(sysExpertReservationParam)) {

            // 根据专家姓名 查询
            if (ObjectUtil.isNotEmpty(sysExpertReservationParam.getName())) {
                queryWrapper.lambda().like(SysExpertReservation::getName, sysExpertReservationParam.getName());
            }
            // 根据擅长领域 查询
            if (ObjectUtil.isNotEmpty(sysExpertReservationParam.getField())) {
                queryWrapper.lambda().like(SysExpertReservation::getField, sysExpertReservationParam.getField());
            }
            // 根据预约状态（0未预约，1已预约，2不可预约） 查询
            if (ObjectUtil.isNotEmpty(sysExpertReservationParam.getStatus())) {
                queryWrapper.lambda().like(SysExpertReservation::getStatus, sysExpertReservationParam.getStatus());
            }
            boolean bol = LoginContextHolder.me().isSuperAdmin();
            pageRecode = sysExpertReservationMapper.page(PageFactory.defaultPage(), queryWrapper);
            if(!bol){
                SysLoginUser sysLoginUser = LoginContextHolder.me().getSysLoginUser();
                for(SysExpertReservation sysExpertReservation:pageRecode.getRecords()){
                    long id = sysExpertReservation.getId();
                    ExpertReservationlog reservationlog = expertReservationlogMapper.queryByIdAnduserId(id,sysLoginUser.getId());
                    if(reservationlog != null){
                        sysExpertReservation.setStatus(reservationlog.getStatus());
                    }
                }
            }
        }
        return new PageResult<>(pageRecode);
    }

    @Override
    public List<SysExpertReservation> list(SysExpertReservationParam sysExpertReservationParam) {
        return this.list();
    }

    @Override
    public void add(SysExpertReservationParam sysExpertReservationParam) {
        SysExpertReservation sysExpertReservation = new SysExpertReservation();
        BeanUtil.copyProperties(sysExpertReservationParam, sysExpertReservation);
        this.save(sysExpertReservation);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SysExpertReservationParam> sysExpertReservationParamList) {
        sysExpertReservationParamList.forEach(sysExpertReservationParam -> {
        SysExpertReservation sysExpertReservation = this.querySysExpertReservation(sysExpertReservationParam);
            this.removeById(sysExpertReservation.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SysExpertReservationParam sysExpertReservationParam) {
        SysExpertReservation sysExpertReservation = this.querySysExpertReservation(sysExpertReservationParam);
        BeanUtil.copyProperties(sysExpertReservationParam, sysExpertReservation);
        this.updateById(sysExpertReservation);
    }

    @Override
    public SysExpertReservation detail(SysExpertReservationParam sysExpertReservationParam) {
        return this.querySysExpertReservation(sysExpertReservationParam);
    }

    @Override
    public void changeStatus(SysExpertReservationParam sysExpertReservationParam) {
        SysLoginUser sysLoginUser = LoginContextHolder.me().getSysLoginUser();
        sysExpertReservationParam.setUserId(sysLoginUser.getId());
        ExpertReservationlog expertReservationlog = new ExpertReservationlog();
        expertReservationlog.setExpertid(sysExpertReservationParam.getId());
        expertReservationlog.setStatus(1);
        expertReservationlog.setUserid(sysLoginUser.getId());
        expertReservationlogService.save(expertReservationlog);
        //sysExpertReservationMapper.saveOrupdateStatusById(sysExpertReservationParam);
        //通知到的人
         Long id = sysExpertReservationParam.getId();
//        Integer noticeUserStatus = SysNoticeUserStatusEnum.UNREAD.getCode();
        //给通知到的人发消息
        //websocket推送
        WebSocketController.sendMessage(sysExpertReservationParam.getContent(), id);
    }

    /**
     * 获取专家预约表
     *
     * @author zhang
     * @date 2024-06-18 11:36:11
     */
    private SysExpertReservation querySysExpertReservation(SysExpertReservationParam sysExpertReservationParam) {
        SysExpertReservation sysExpertReservation = this.getById(sysExpertReservationParam.getId());
        if (ObjectUtil.isNull(sysExpertReservation)) {
            throw new ServiceException(SysExpertReservationExceptionEnum.NOT_EXIST);
        }
        return sysExpertReservation;
    }
}
